package com.fdb.efp.console.api.rest;

import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.SEQ;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.basic.sequence.service.DefaultPatternDescriptor;
import com.irdstudio.basic.sequence.service.PatternDescriptor;
import com.irdstudio.basic.sequence.service.facade.PatternedLimitableSeqService;
import com.irdstudio.efp.console.service.facade.PrdSoltRuleService;
import com.irdstudio.efp.console.service.vo.PrdSoltRuleVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.core.util.SpringContextUtils;
import com.irdstudio.basic.framework.core.util.TimeUtil;
import com.irdstudio.basic.framework.web.controller.AbstractController;

/**
 * Controller：产品规则插槽参数
 * 
 * @author huangwb
 * @since 2018年12月26日 下午7:42:54
 * @version 1.0
 */
@RestController
@RequestMapping("/api")
public class PrdSoltRuleController extends AbstractController {
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	@Autowired
	@Qualifier("prdSoltRuleService")
	private PrdSoltRuleService prdSoltRuleService;
	public static final PatternDescriptor PATTERN = DefaultPatternDescriptor.of(SEQ, 9);
	/**
	 * 列表数据查询
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	@PostMapping(value = "/prd/solt/rules" )
	public @ResponseBody ResponseData<List<PrdSoltRuleVO>> queryPrdSoltRuleAll(@RequestBody PrdSoltRuleVO vo) {
		List<PrdSoltRuleVO> outputVo = prdSoltRuleService.queryPrdSoltRuleList(vo);
		return getResponseData(outputVo);
	}

	/**
	 * 根据主键查询详情
	 * 
	 * @return
	 */
	@GetMapping(value = "/prd/solt/rule/{recordKeyid}")
	public @ResponseBody ResponseData<PrdSoltRuleVO> queryByPk(@PathVariable("recordKeyid") String recordKeyid) {
		PrdSoltRuleVO inVo = new PrdSoltRuleVO();
		inVo.setRecordKeyid(recordKeyid);
		PrdSoltRuleVO outputVo = prdSoltRuleService.queryByPk(inVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键删除信息
	 * 
	 * @param prdSoltRule
	 * @return
	 */

	@DeleteMapping(value = "/prd/solt/rule")
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody PrdSoltRuleVO inPrdSoltRuleVo) {
		int outputVo = prdSoltRuleService.deleteByPk(inPrdSoltRuleVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * 
	 * @param inPrdSoltRuleVo
	 * @return
	 */
	@PutMapping(value = "/prd/solt/rule")
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody PrdSoltRuleVO inPrdSoltRuleVo) {
		setUserInfoToVO(inPrdSoltRuleVo);
		inPrdSoltRuleVo.setLastModifyUser(inPrdSoltRuleVo.getLoginUserId());
		inPrdSoltRuleVo.setLastModifyTime(TimeUtil.getCurrentDateTime());
		int outputVo = prdSoltRuleService.updateByPk(inPrdSoltRuleVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * 
	 * @param inPrdSoltRuleVo
	 * @return
	 * @throws Exception 
	 */
	@PostMapping(value = "/prd/solt/rule")
	public @ResponseBody ResponseData<Integer> insertPrdSoltRule(@RequestBody PrdSoltRuleVO inPrdSoltRuleVo) throws Exception {
		setUserInfoToVO(inPrdSoltRuleVo);
		inPrdSoltRuleVo.setRecordKeyid(prdSoltRuleID());
		inPrdSoltRuleVo.setLastModifyUser(inPrdSoltRuleVo.getLoginUserId());
		inPrdSoltRuleVo.setLastModifyTime(TimeUtil.getCurrentDateTime());
		int outputVo = prdSoltRuleService.insertPrdSoltRule(inPrdSoltRuleVo);
		return getResponseData(outputVo);

	}

	public static String prdSoltRuleID() throws Exception {
		PatternedLimitableSeqService sequenceService = 
				SpringContextUtils.getBean(PatternedLimitableSeqService.class);
		return sequenceService.getPatternedSequence("PRD_SOLT_RULE_ID", PATTERN);
	}
}
